<?php

namespace App\Module;

use App\depository\MarkDep;
use App\Enum\MarkEnum;
use Illuminate\Support\Facades\Auth;
use App\Service\DictService;

class MarkModule extends BaseModule
{
    public function init()
    {
        $dictService = new DictService();
        $dictService->setUsers()->setTeams();

        $data = [
            'dict' => $dictService->getDict(),
        ];

        return self::response($data);
    }

    public function add($request)
    {
        $param = $request->input();
        $dep = new MarkDep();
        $resdep = $dep->find(['user_id' => Auth::id(), 'steamid' => $param['steamid']]);
        $data = [];
        if (!empty($resdep)) {
            $data = [
                'user_id' => Auth::id(),
                'steamid' => $param['steamid'],
                'content' => $param['content'],
                'jurisdiction' => $param['jurisdiction'],
                'gh_id' => $param['gh_id'],
            ];
            $dep->upd($resdep['id'], $data);
        } else {
            $data = [
                'user_id' => Auth::id(),
                'steamid' => $param['steamid'],
                'content' => $param['content'],
                'jurisdiction' => $param['jurisdiction'],
                'gh_id' => $param['gh_id'],
            ];
            $dep->add($data);
        }

        return self::response();
    }

    public function list($request, $start = true)
    {
        $param = $request->input();
        $param['user_id'] = Auth::id();
        $dep = new MarkDep();
        $resList = $dep->list($param, $start);

        $data['list'] = $resList->map(function ($item) {
            return [
                'id' => $item->id,
                'name' => $item->name,
                'steamid' => $item->steamid,
                'content' => $item->content,
                'gh_name' => $item->gh_name,
                'jurisdiction' => $item->jurisdiction,
                'jurisdiction_show' => MarkEnum::$juArr[$item->jurisdiction],
                'date' => date($item->created_at)
            ];
        });

        $data['page'] = [
            'per_page' => $param['per_page'],
            'current_page' => $param['current_page'],
            'total_page' => $resList->lastPage(),
            'total' => $resList->total(),
        ];
        return self::response($data);
    }

    public function del($request)
    {
        $param = $request->input();
        $dep = new MarkDep();
        $userid = $dep->find(['id' => $param['id']]);

        if ($userid['user_id'] != Auth::id()) {
            return self::response([], '没有删除权限', 100);
        }
        $dep->del($param['id']);
        return self::response();
    }

    public function adminDel($request){
        $param = $request->input();

        $dep = new MarkDep();
        $dep->del($param['id']);
        return self::response();
    }
}
